<?php
/**
 * 新增到个人用户
 * @author xulei
 * @since v1.0
 * @modify 2014.9.25
 * @copyright 深圳亿游
 */
require_once(LIB_PATH.'controller/root.con.php');

class qrCon extends rootCon
{
    public function createAction(){
        $scene = r('scene');;
        $page  = r('page');
        $width = r('width');
        if(!$width) $width = 1280;
        $fileName = myxiaochengxu::qrTemp($scene,$page,$width);
        $this->j(0,"",['file'=>$fileName]);
    }

    private function shareCourse($courseId){
        $uid = mylogin::getUid(true);
        if(!$courseId){
            if( r('isDebug')){
                $courseId = 19840521;
            }else{
                $this->j(-1,"缺少参数",['courseId',$courseId]);
            }
        }

        $course = new Course();
        $find = $course->find($courseId);
        if(!$find || $course->status!= STATUS_PUBLISH){
            $this->j(-2,"参数错误",['find'=>$find,'status'=>$course->status]);
        }

        $scene =  "c=$courseId&f=".$uid."&a=".r('a');
        if(r('page')){
            $page  = r('page');
        }else{
            $page  = "pages/classdetail/index";
        }

        $startX = 100;
        $startY = 24;;
        myimage::setSmallFont();
        $title = r('title');
        if(!$title) {
            $title = "这个培训课程不错，有小孩的朋友可以看看";
        }
        myimage::writeText($title,$startX,$startY);

        myimage::setBoldFont();
        $title = mystring::split($course->courseName,15);
        $titleLine = mystring::getLineCount();
        myimage::writeText($title,20,650);

        $school = new School();
        $school->find($course->schoolId);
        myimage::setSmallFont();
        $addr = mystring::split($school->addr,16);
        myimage::setFontSize(18);
        myimage::writeText($addr,20,650+ $titleLine * 40);

        //-----------头图--------------
        $marginTopImage  = 60;//需要合并过去的图片
        $newSmallHeight = 561;//合并过去以后的高度
        $image_1 = myimage::createFromAny($course->uploadImg);
        myimage::copyToCenter($image_1,$newSmallHeight,$marginTopImage);

        //----------积分----------
        if(!$course->scoreCount){
            $imageStar = myimage::createFromAny(ROOT_PATH."web/img/star50.png");
        }else{
            $score = round($course->totalScore * 10);
            if($score<30) $score = 30;//最低显示3星
            if($score>50) $score = 50;
            $imageStar = myimage::createFromAny(ROOT_PATH."web/img/star$score.png");
        }
        myimage::copyToPos($imageStar,600,640);
        myimage::setFontSize(15);
        myimage::writeText($course->learned."人学过",615,680,"0xcc","0xcc","0xcc");

        //--------二维码图片---------
        if(IS_WINDOWS){
            $qrFile              = "up/2.png";//二维码
        }else{
            $qrFile              = myxiaochengxu::qrTemp($scene,$page);//二维码
        }
        $imageQr = myimage::createFromAny($qrFile);
        myimage::copyToPos($imageQr,90,790);
    }

    public function shareAction(){
        myimage::init(750,1100);

        //----模板背景-------
        $templateId = 1;
        $template = new QrTemplate();
        if(r('qrTemplateId')){
            $template->qrTemplateId = r('qrTemplateId');
        }else{
            $template->qrTemplateId =$templateId;
        }
        $find =  $template->find();
        if(!$find){
            j(["code"=>-1,"info"=>"没有找到模板",['qrTemplateId'=>$templateId]]);
        }
        if($template->img && $template->status==STATUS_PUBLISH){
            $templateImg =  myimage::createFromAny(ROOT_PATH."web/".$template->img);
            myimage::copyToCenter($templateImg,1100);
        }

        $courseId  = r('c')?r_int('c'):r_int('courseId');
        $schoolId  = r('s')?r_int('s'):r_int('schoolId');
        $teacherId = r('t')?r_int('t'):r_int('teacherId');
        if($courseId) {
            $this->shareCourse($courseId);
        }else if($schoolId){
            $this->shareSchool($schoolId);
        }else if($teacherId){
            $this->shareTeacher($teacherId);
        }else{
            $this->j(-3,"error parameter",['courseId'=>$courseId,'teacherId'=>$teacherId,'schoolId'=>$schoolId]);
        }


        //----------二维码文字---------
        myimage::setFontSize(20);
        myimage::writeText("长按识别，了解详情",400,910);

        if(!r('noImage')){
            myimage::showJpeg();
        }
        myimage::clear();
    }

    private function shareSchool($schoolId){
        $uid = mylogin::getUid(true);
        $school = new School();
        $find = $school->find($schoolId);
        if(!$find || $school->status!= STATUS_PUBLISH){
            $this->j(-2,"参数错误",['find'=>$find,'status'=>$school->status]);
        }

        $scene =  "s=$schoolId&f=".$uid."&a=".r('a');
        if(r('page')){
            $page  = r('page');
        }else{
            $page  = "pages/institufirst/index";
        }
        


        $startX = 150;
        $startY = 24;;
        myimage::setSmallFont();

        $title = r('title');
        if(!$title) {
            $title = "这个培训学校不错，推荐你了解一下";
        }
        myimage::writeText($title,$startX,$startY);

        myimage::setBoldFont();
        $title = mystring::split($school->schoolName,15);
        $titleLine = mystring::getLineCount();
        myimage::writeText($title,20,650);

        myimage::setSmallFont();
        $addr = mystring::split($school->addr,16);
        myimage::setFontSize(18);
        myimage::writeText($addr,20,650+ $titleLine * 40);

        //-----------头图--------------
        $marginTopImage  = 60;//需要合并过去的图片
        $newSmallHeight = 561;//合并过去以后的高度
        $image_1 = myimage::createFromAny($school->logoUrl);
        myimage::copyToCenter($image_1,$newSmallHeight,$marginTopImage);

        //----------积分----------
        if(!$school->scoreCount){
            $imageStar = myimage::createFromAny(ROOT_PATH."web/img/star50.png");
        }else{
            $score = round($school->finalScore * 10);
            if($score<30) $score = 30;//最低显示3星
            if($score>50) $score = 50;
            $imageStar = myimage::createFromAny(ROOT_PATH."web/img/star$score.png");
        }
        myimage::copyToPos($imageStar,600,640);
        myimage::setFontSize(15);
        myimage::writeText($school->learned."人学过",615,680,"0xcc","0xcc","0xcc");

        //--------二维码图片---------
        if(IS_WINDOWS){
            $qrFile              = "up/2.png";//二维码
        }else{
            $qrFile              = myxiaochengxu::qrTemp($scene,$page);//二维码
        }
        $imageQr = myimage::createFromAny($qrFile);
        myimage::copyToPos($imageQr,90,790);
    }

    private function shareTeacher($teacherId){
        $uid = mylogin::getUid(true);
        $teacher = new Teacher();
        $find = $teacher->find($teacherId);
        if(!$find || $teacher->status!= STATUS_PUBLISH){
            $this->j(-2,"参数错误",['find'=>$find,'status'=>$teacher->status]);
        }

        $scene =  "t=$teacherId&f=".$uid."&a=".r('a');
        if(r('page')){
            $page  = r('page');
        }else{
            $page  = "pages/teacherpage/teacherpage";
        }

        $startX = 100;
        $startY = 24;;
        myimage::setSmallFont();
        $title = r('title');
        if(!$title) {
            $title = "这个老师挺靠谱，教学质量也挺高，你了解一下。";
        }
        myimage::writeText($title,$startX,$startY);

        myimage::setBoldFont();
        $title = mystring::split($teacher->teacherName,15);
        $titleLine = mystring::getLineCount();
        myimage::writeText($title,20,650);

        $school = new School();
        $school->find($teacher->schoolId);
        myimage::setSmallFont();
        $addr = mystring::split($school->addr,16);
        myimage::setFontSize(18);
        myimage::writeText($addr,20,650+ $titleLine * 40);

        //-----------头图--------------
        $marginTopImage  = 60;//需要合并过去的图片
        $newSmallHeight = 561;//合并过去以后的高度
        $image_1 = myimage::createFromAny($teacher->uploadImg);
        myimage::copyToCenter($image_1,$newSmallHeight,$marginTopImage);

        //----------积分----------
        if(!$teacher->scoreCount){
            $imageStar = myimage::createFromAny(ROOT_PATH."web/img/star50.png");
        }else{
            $score = round($teacher->finalScore * 10);
            if($score<30) $score = 30;//最低显示3星
            if($score>50) $score = 50;
            $imageStar = myimage::createFromAny(ROOT_PATH."web/img/star$score.png");
        }
        myimage::copyToPos($imageStar,600,640);
        myimage::setFontSize(15);
        myimage::writeText($teacher->learned."人学过",615,680,"0xcc","0xcc","0xcc");

        //--------二维码图片---------
        if(IS_WINDOWS){
            $qrFile              = "up/2.png";//二维码
        }else{
            $qrFile              = myxiaochengxu::qrTemp($scene,$page);//二维码
        }
        $imageQr = myimage::createFromAny($qrFile);
        myimage::copyToPos($imageQr,90,790);
    }

    public function createFullAction(){
        $uid = mylogin::getUid(true);

        $scene = r('scene')?r('scene'):"fromUid=$uid";
        $page  = r('page')?r('page'):"pages/index/index";
        $title = r('title')?r('title'):"title参数将会显示在这里";
        $desc  = r('desc')?r('desc'):"desc参数内容将会显示在这里";

        $qrFile              = myxiaochengxu::qrTemp($scene,$page);//二维码
        $promotionFile       = myxiaochengxu::promotionQr(r('qrTemplateId'),$qrFile,$title,$desc);

        //$this->j(0,"",['file'=>FRONT_URL.$promotionFile,'qr'=>FRONT_URL.$qrFile]);
    }

}